﻿using System;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using OmegaEngine;

namespace OmegaEngine
{
    public class GUILabelButton : GUIActor
    {
        string text = "";
        SpriteFont font = Engine.DefaultFont;
        Color color = Color.White;
        GUIButtonHandler action = null;
        bool isMouseOver = false;

        public GUILabelButton(string Text, GUIButtonHandler Action)
            : base()
        {
            SetupLabel(Text, Action, Engine.DefaultFont, Color.White, 0, 0);
        }

        public GUILabelButton(string Text, GUIButtonHandler Action, SpriteFont Font)
            : base()
        {
            SetupLabel(Text, Action, Font, Color.White, 0, 0);
        }

        public GUILabelButton(string Text, GUIButtonHandler Action, SpriteFont Font, int PositionX, int PositionY)
            : base(1, 1, PositionX, PositionY)
        {
            SetupLabel(Text, Action, Font, Color.White, PositionX, PositionY);
        }

        public GUILabelButton(string Text, GUIButtonHandler Action, SpriteFont Font, int PositionX, int PositionY, Color Color)
            : base(1, 1, PositionX, PositionY)
        {
            SetupLabel(Text, Action, Font, Color, PositionX, PositionY);
        }

        public GUILabelButton(string Text, GUIButtonHandler Action, SpriteFont Font, int PositionX, int PositionY, Color Color, GameScreen Parent)
            : base(1, 1, PositionX, PositionY, Parent)
        {
            SetupLabel(Text, Action, Font, Color, PositionX, PositionY);
        }

        void SetupLabel(string Text, GUIButtonHandler Action, SpriteFont Font, Color Color, int PositionX, int PositionY)
        {
            this.text = Text;
            this.font = Font;
            this.color = Color;
            this.checkInput = true;
            this.action = Action;

            // TODO: Re-Work this width/height stuff into the constructors

            this.positionX = PositionX;
            this.positionY = PositionY;

            Vector2 size = Font.MeasureString(Text);
            this.Rectangle = new Rectangle(PositionX, PositionY, (int)size.X, (int)size.Y);
        }

        public void UpdateText(string Text)
        {
            this.text = Text;

            Vector2 size = this.font.MeasureString(Text);

            Rectangle tempRect = this.Rectangle;
            this.Rectangle = new Rectangle(tempRect.X, tempRect.Y, (int)size.X, (int)size.Y);
        }

        public override void Update()
        {
            MouseDevice Mouse = Engine.Services.GetService<MouseDevice>();

            this.isMouseOver = this.Rectangle.Contains((int)Mouse.Position.X, (int)Mouse.Position.Y);

            if (this.isMouseOver)
            {
                this.color.A = 128;
            }
            else
            {
                this.color.A = 255;
            }

            if (this.isMouseOver && Mouse.WasButtonPressed(MouseButtons.Left))
            {
                //Parent.Disable();
                this.action();
            }
        }

        public override void Draw()
        {
            // SpriteFont.MeasureString
            // this.font.MeasureString(this.text);
            Engine.SpriteBatch.DrawString(this.font, this.text, new Vector2(this.positionX, this.positionY), this.color);
            base.Draw();
        }
    }
}
